//导入
const mongoose = require('mongoose');
const fs = require('fs');
//连接
mongoose.connect('mongodb://127.0.0.1:27017/games');

//绑定回调
mongoose.connection.on('open', () => {
  //创建结构对象
  const MovieSchema = new mongoose.Schema({
    name: {
      type: String,
      required: true
    },
    director: {
      type: String,
      required: true
    },
    actors: {
      type: Array
    },
    rate: {
      type: Number,
      default: 0
    },
    pub_time: {
      type: Date
    }
  })

  //创建模型对象
  const MovieModel = mongoose.model('movie', MovieSchema);

  //更新文档
  // MovieModel.updateOne({_id: '64390bf9f5128113d84a9ec4'}, {rate: 9.8}, (err, data) => {
  //   if(err) throw err;
  //   console.log(data);
  //   //关闭链接
  //   mongoose.disconnect();
  // });

  //批量更新
  MovieModel.updateMany({director: '开心麻花'}, {director: '开心麻辣烫'}, (err, data) => {
    if(err) throw err;
    console.log(data);
    //关闭链接
    mongoose.disconnect();
  });

});


mongoose.connection.on('error', () => {
  console.log('连接失败~~');
});